
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#ifndef __AVIFILE_H
#define __AVIFILE_H

#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include <vfw.h>
#include <TChar.h>

#include "RTTypes.h"

namespace RT
{

	/*Create an avi file and add frames to it.*/
	class RTAviFile
	{
		HDC					m_hAviDC;
		HANDLE				m_hHeap;
		LPVOID				m_lpBits;					// Useful for holding the bitmap content, if any
		LONG				m_lSample;					// Keeps track of the current Frame Index
		PAVIFILE			m_pAviFile;
		PAVISTREAM			m_pAviStream;
		PAVISTREAM			m_pAviCompressedStream;
		AVISTREAMINFO		m_AviStreamInfo;
		AVICOMPRESSOPTIONS	m_AviCompressOptions;
		DWORD				m_dwFrameRate;				// Frames Per Second Rate (FPS)
		DWORD				m_dwFCCHandler;				// Video Codec FourCC	
		TCHAR				m_szFileName[MAX_PATH];		// Holds the Output Movie File Name
		TCHAR				m_szErrMsg[MAX_PATH];		// Holds the Last Error Message, if any
	
		I32					m_nAppendFuncSelector;		//0=Dummy	1=FirstTime	2=Usual

		HRESULT	AppendFrameFirstTime(HBITMAP );
		HRESULT	AppendFrameUsual(HBITMAP);
		HRESULT	AppendDummy(HBITMAP);
		HRESULT	(RTAviFile::*pAppendFrame[3])(HBITMAP hBitmap);


		HRESULT	AppendFrameFirstTime(I32, I32, LPVOID,I32 );
		HRESULT	AppendFrameUsual(I32, I32, LPVOID,I32 );
		HRESULT	AppendDummy(I32, I32, LPVOID,I32 );
		HRESULT	(RTAviFile::*pAppendFrameBits[3])(I32, I32, LPVOID, I32);

		/// Takes care of creating the memory, streams, compression options etc. required for the movie
		HRESULT InitMovieCreation(I32 nFrameWidth, I32 nFrameHeight, I32 nBitsPerPixel);

		/// Takes care of releasing the memory and movie related handles
		void ReleaseMemory();

		/// Sets the Error Message
		void SetErrorMessage(LPCTSTR lpszErrMsg);

	public:
		/// <Summary>
		/// Constructor accepts the filename, video code and frame rate settings
		/// as parameters.
		/// lpszFileName: Name of the output movie file to create
		/// dwCodec: FourCC of the Video Codec to be used for compression
		/// dwFrameRate: The Frames Per Second (FPS) setting to be used for the movie
		/// </Summary>
		/// <Remarks>
		/// The default Codec used here is MPG4. To use a different codec, pass its Fourcc
		/// value as the input parameter for dwCodec. 
		/// For example,
		/// pass mmioFOURCC('D','I','V','X') to use DIVX codec, or
		/// pass mmioFOURCC('I','V','5','0') to use IV50 codec etc...
		///
		/// Also, you can pass just 0 to avoid the codecs altogether. In that case, Frames 
		/// would be saved as they are without any compression; However, the output movie file
		/// size would be very huge in that case.
		///
		/// Finally, make sure you have the codec installed on the machine before 
		/// passing its Fourcc here.
		/// </Remarks>
		RTAviFile(LPCTSTR lpszFileName=_T("Output.avi"), 
				DWORD dwCodec = mmioFOURCC('M','P','G','4'),
				DWORD dwFrameRate = 1);

		/// <Summary> 
		/// Destructor closes the movie file and flushes all the frames
		/// </Summary>
		~RTAviFile(void);

		/// </Summary>
		/// Inserts the given HBitmap I32o the movie as a new Frame at the end.
		/// </Summary>
		HRESULT	AppendNewFrame(HBITMAP hBitmap);

		/// </Summary>
		/// Inserts the given bitmap bits I32o the movie as a new Frame at the end.
		/// The width, height and nBitsPerPixel are the width, height and bits per pixel
		/// of the bitmap poI32ed to by the input pBits.
		/// </Summary>
		HRESULT	AppendNewFrame(I32 nWidth, I32 nHeight, LPVOID pBits,I32 nBitsPerPixel);

		/// <Summary>
		/// Returns the last error message, if any.
		/// </Summary>
		LPCTSTR GetLastErrorMessage() const {	return m_szErrMsg;	}
	};

}
#endif